package com.rensea.android.objs;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.rensea.android.util.Constants;
import com.rensea.android.util.LangUtils;

public class ApiObjectUtils {
	public static Message[] createMessageAFromJson(JSONArray jsonArray){
		if(null==jsonArray)return new Message[0];
		Message[] msgs =  new Message[jsonArray.length()];
		for(int i=0;i<jsonArray.length();i++){
			try {
				msgs[i] = createMessageFromJson(jsonArray.getJSONObject(i));
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return msgs;
	}
	public static Message createMessageFromJson(JSONObject jsonObj) {
		Message message =  new Message();
		message.setUser(createUserFromJson(getJSONObject(jsonObj,"user")));
		message.setId(getLong(jsonObj,"id"));
		message.setStatusType(getString(jsonObj,"status_type"));
		message.setLinkTitle(getString(jsonObj,"link_title"));
		message.setText(getString(jsonObj,"text"));
		message.setOriginalUrl(getString(jsonObj,"original_url"));
		message.setSource(getString(jsonObj,"source"));
		
		message.setCreatedAt(getDate(jsonObj,"created_at"));
		
		message.setInReplyToStatusId(getLong(jsonObj,"in_reply_to_status_id"));
		message.setFavorited(getBoolean(jsonObj,"favorited"));

//		Log.d(Constants.LOG_TAG, jsonObj.toString());
//		if(message.getUser().getScreenName().equalsIgnoreCase("maven")){
//		}
		return message;
	}
	private static Map<Long,User> users = new HashMap<Long,User>();
	public static User createUserFromJson(JSONObject jsonObj) {
		if(null==jsonObj)return null;
		long id = getLong(jsonObj,"id");
		if(users.containsKey(id)){
			return users.get(id);
		}
		User user =  new User();
		user.setId(id);
		user.setLocation(getString(jsonObj,"location"));
		user.setFollowingCount(getLong(jsonObj,"following_count"));
		user.setUrl(getString(jsonObj,"url"));
		user.setUserProtected(getBoolean(jsonObj,"protected"));
		user.setFavouritesCount(getLong(jsonObj,"favourites_count"));
		user.setDescription(getString(jsonObj,"description"));
		user.setName(getString(jsonObj,"name"));
		user.setCreatedAt(getDate(jsonObj,"created_at"));
		user.setScreenName(getString(jsonObj,"screen_name"));
		user.setProfileImageUrl(getString(jsonObj,"profile_image_url"));
		user.setFollowersCount(getLong(jsonObj,"followers_count"));
		user.setFollowing(getLong(jsonObj, "is_following")==1);
		users.put(id, user);
//		System.out.println(user.getProfileImageUrl());
		return user;
	}

	private static JSONObject getJSONObject(JSONObject jsonObj, String key) {
		try {
			return jsonObj.getJSONObject(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String getString(JSONObject jsonObj, String key) {
		try {
			if(!jsonObj.isNull(key))
				return jsonObj.getString(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	private static long getLong(JSONObject jsonObj,String key){
		try {
			if(!jsonObj.isNull(key))
				return jsonObj.getLong(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return 0;
	}
	private static final String DateFormat = "yyyy-MM-dd HH:mm:ss Z";
	private static final SimpleDateFormat format = new SimpleDateFormat(DateFormat);
	//2009-04-30 11:27:08
	private static Date getDate(JSONObject jsonObj,String key){
		try {
			String dateStr = jsonObj.getString(key);
			if(!LangUtils.isEmpty(dateStr)){
				return format.parse(dateStr);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	private static boolean getBoolean(JSONObject jsonObj,String key){
		try {
			return jsonObj.getBoolean(key);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return false;
	}
}
